package manage.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import manage.dao.AccumulationFundMapper;
import manage.model.AccumulationFund;
import manage.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccumulationFundServiceImpl {

    @Autowired
    private AccumulationFundMapper accumulationFundMapper;

    public PageInfo<AccumulationFund> selectAll(String page, String limit) {

        int pu = Integer.parseInt(page);
        int li = Integer.parseInt(limit);

        PageHelper.startPage(pu, li);
        List<AccumulationFund> list = accumulationFundMapper.selectAll();
        PageInfo<AccumulationFund> pageInfo = new PageInfo<AccumulationFund>(list);
        return pageInfo;
    }

    public String insert(AccumulationFund record) {

        int i = accumulationFundMapper.insert(record);
        if (i > 0)
            return "success";
        return "fail";
    }

    public String del(Integer id) {
        int i = accumulationFundMapper.deleteByPrimaryKey(id);
        if (i > 0)
            return "success";
        return "fail";
    }

    public AccumulationFund select(Integer id) {
        return accumulationFundMapper.selectByPrimaryKey(id);
    }

    public String update(AccumulationFund record) {
        if (accumulationFundMapper.updateByPrimaryKey(record) > 0)
            return "success";
        return "fail";
    }

    public String InputExcel(InputStream is, String originalFilename) {
        Map<String, Object> ginsengMap = new HashMap<String, Object>();
        List<ArrayList<Object>> list;
        if (originalFilename.endsWith(".xls")) {
            list = ExcelUtil.readExcel2003(is);
            System.out.println(2003);
        } else {
            System.out.println(2007);
            list = ExcelUtil.readExcel2007(is);
        }
        System.out.println(list.size());
        for (int i = 0 ; i < list.size(); i++) {
            List<Object> row = list.get(i);
            ginsengMap.put("userAcount", row.get(0).toString());
            ginsengMap.put("money", Integer.parseInt(row.get(1).toString()));
            ginsengMap.put("date", row.get(2).toString());
            ginsengMap.put("acount", row.get(3).toString());
            ginsengMap.put("insurance", Integer.parseInt(row.get(4).toString()));
            accumulationFundMapper.insert2(ginsengMap);
        }
        System.out.println("01");
        return "01";
    }
}